package de.koller.worlders.engine.entity.zone;

import de.koller.worlders.WSea;
import de.koller.worlders.engine.entity.mo.npc.Slime01;
import de.koller.worlders.engine.tiles.ITile;
import de.koller.worlders.engine.tiles.data.ITileCreator;
import de.koller.worlders.render.VisualMap;
import de.koller.worlders.render.visual.DisplayRenderObject;

public class Spawner extends ZoneEffect {
	public static final int GEN_ID = 4;
	
	public static final ITileCreator GEN_INSTANCE = new ITileCreator() {
		@Override public ITile newInstance() {
			return new Zone().addEffect( new Spawner( MobType.Default ) );
		}
		
		@Override public DisplayRenderObject getIcon() { return VisualMap.spawner; }
		@Override public String getName() { return "Spawner"; }
	};
	
	private final MobType spw;
	private final SpawnCounter spawnCounter = new SpawnCounter();
	
	public Spawner( MobType spw ) {
		this.spw = spw;
	}
	
	@Override
	public void init( Zone z ) {
		super.init( z );
		
//		if( spw == MobType.Default ) z.setTextureRep( new TextureRep( "data/tiles.png", 160, 32, 32, 32 ) );
		if( spw == MobType.Default ) z.getTile().setVisual( VisualMap.spawner );
		
		if( zone.root == null )
			return;
		
		zone.root.getEntityContainer().addChild( spawnCounter );
	}

	@Override
	public void release() {
		super.release();
		
		zone.root.getEntityContainer().removeChild( spawnCounter );
	}
	
	private static enum MobType {
		Default;
	}
	
	private class SpawnCounter extends WSea {
		private final int spawnInterval = 60000;
		private long nextSpawn = 0;

		@Override
		protected void update( int elapsedTime ) {
			super.update( elapsedTime );
			
			if( wroot.getDebug().isActive() ) {
				nextSpawn = wroot.getTime() + 1000;
				return;
			}
			
			if( nextSpawn < wroot.getTime() ) {
				nextSpawn = wroot.getTime() + spawnInterval;
				wroot.getEntityContainer().getResetNode().addChild( new Slime01( zone.getPosition().getXf(), zone.getPosition().getYf() ) );
			}
		}

		@Override
		protected void resetNodeNotify() {
			super.resetNodeNotify();
			
			nextSpawn = wroot.getTime() + 1000;
		}

	}
}
